package yxy.cherry.battle;

import java.io.File;
import java.io.FileOutputStream;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.alibaba.fastjson.parser.ParserConfig;

import yxy.apple.util.FileUtility;
import yxy.apple.util.ReflectionUtility;
import yxy.apple.util.redisson.FastjsonCodec;
import yxy.apple.util.redisson.RedisConnection;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.battle.module.affix.SkillType;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.affixtype;
import yxy.cherry.data.script.FomulaScript;
import yxy.cherry.game.base.redis.RedisManager;
import yxy.cherry.game.base.redis.RedisType;

public class AffixFileMaker {

	public static void main(String[] args) {
		ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
		try {

			{
				RedisConnection redisData = new RedisConnection();
				redisData.connect("redis://127.0.0.1:6379", "123456", 15, true, new FastjsonCodec());
				RedisManager.getInstance().addConnection(RedisType.REDIS_DATA, redisData);

				DataCenter.init(redisData, false);
				FomulaScript.init(redisData);
			}
		} catch (RedisException e) {
			e.printStackTrace();
		}
		Set<String> doneSet = new HashSet<>();
		Set<Class<?>> affixTypeSet = ReflectionUtility.getTypesAnnotatedWith("yxy.cherry.battle.module.affix", SkillType.class);
		for (Class<?> clazz : affixTypeSet) {
//			int type = clazz.getAnnotation(SkillType.class).Type();
//			doneSet.add(String.valueOf(type));
		}

		String DIR = "./src/test/java";
		String packageName = "yxy.cherry.battle.module.affix";
		AffixFileMaker maker = new AffixFileMaker();

		Map<String, affixtype> datas = DataCenter.getAllData(affixtype.class);
		for (affixtype data : datas.values()) {
			if (doneSet.contains(data.getCode())) {
				continue;
			}
			try {
				System.err.println(data.getId());
				maker.makeFile(DIR, packageName, data);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		System.exit(0);
	}

	void makeFile(String dir, String packageName, affixtype data) throws Exception {
//		String packageName = "yxy.cherry.battle.module.target";
		String filename = "Affix_" + data.getId();

		String path = dir + "/" + packageName.replaceAll("\\.", "/");
		File output = new File(path + "/" + filename + ".java");
		FileUtility.create(output);
		String content = assembleContent(packageName, data);
		FileOutputStream fos = new FileOutputStream(output);
		fos.write(content.getBytes("UTF-8"));
		fos.flush();
		fos.close();
	}

	private String assembleContent(String packageName, affixtype data) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("package " + packageName + ";\r\n\r\n");
		buffer.append("import yxy.cherry.battle.Fighter;\r\n");
		buffer.append("import yxy.cherry.battle.module.affix.Affix;\r\n");
		buffer.append("import yxy.cherry.battle.module.affix.AffixType;\r\n");
		buffer.append("import yxy.cherry.battle.module.record.Record;\r\n");
		buffer.append("import yxy.cherry.battle.module.selector.TargetSelector;\r\n\r\n");
		buffer.append("/** " + data.getDescription() + " */\r\n");
		buffer.append("@AffixType(Type = " + data.getId() + ")\r\n");
		buffer.append("public class Affix_" + data.getId() + " extends Affix {\r\n");
		buffer.append("	// TODO 词缀功能需要完善\r\n");
		if (!data.getCondition().equals("-1")) {
			buffer.append("\r\n	//条件：		" + data.getCondition());
		}
		if (!data.getPromote1().equals("-1")) {
			buffer.append("\r\n	//参数1:	" + data.getPromote1());
		}
		if (!data.getPromote2().equals("-1")) {
			buffer.append("\r\n	//参数2:	" + data.getPromote2());
		}

		buffer.append("\r\n");
		buffer.append("	public Affix_" + data.getId() + "(TargetSelector selectors, int bufferId, int condition, int[] params) {\r\n");
		buffer.append("		super(selectors, bufferId, condition, params);\r\n");
		buffer.append("	}\r\n");
		buffer.append("	@Override\r\n");
		buffer.append("	public int getType() {\r\n");
		buffer.append("		return " + data.getId() + ";\r\n");
		buffer.append("	}\r\n");
		buffer.append("	@Override\r\n");
		buffer.append("	public boolean exec(Fighter actor, Fighter target) {\r\n");
		buffer.append("		Record.stdout(actor.getName() + \" 对  \" + target.getName() + \" AFFIX:\" + getType() + \" " + data.getDescription() + "  \");\r\n");
		buffer.append("		return false;\r\n");
		buffer.append("	}\r\n");
		buffer.append("\r\n");
		buffer.append("}");
		return buffer.toString();
	}

}
